package com.jams.goodox.activity.serve.visitor;

import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jams.goodox.BaseActivity;
import com.jams.goodox.R;
import com.jams.goodox.entity.VisitorEwmBean;
import com.jams.goodox.utils.StyleUtil;
import com.jams.goodox.utils.Utils;

import butterknife.Bind;
import butterknife.OnClick;
import uama.share.ShareView;

/**
 * Created by jams_zhen on 2016/9/26.
 *  访客二维码页面
 */

public class VisitorEwmActivity extends BaseActivity {

    @Bind(R.id.tx_communityName)
    TextView txCommunityName;
    @Bind(R.id.tx_name)
    TextView txName;
    @Bind(R.id.tx_carNumber)
    TextView txCarNumber;
    @Bind(R.id.tx_code)
    TextView txCode;
    @Bind(R.id.img_code)
    ImageView imgCode;
    @Bind(R.id.tx_comeDate)
    TextView txComeDate;
    @Bind(R.id.my_share)
    ShareView myShare;

    private Bitmap jiepingMap;
    private Bitmap bitmap;
    String msg = "kkfksdk";

    private static final String SEND_MSG="%s尊敬的贵宾，欢迎您在%s光临%s，您的通行码为：%s，进入小区时请出示给门岗。通行码当天有效，我们将竭诚为您服务！";

    @Override
    protected int getLayoutId() {
        return R.layout.visitor_ewm_activity;
    }

    @Override
    protected void init() {
        VisitorEwmBean.DataBean visitorEwmBean = (VisitorEwmBean.DataBean) getIntent().getSerializableExtra("visitorEwmBean");
        if (visitorEwmBean == null || TextUtils.isEmpty(visitorEwmBean.getPassCode()))
            return;
        bitmap = Utils.buildQRCode(imgCode, visitorEwmBean.getPassCode(), this);
        StyleUtil.titleBackKey(this, "通行证");
        if(TextUtils.isEmpty(visitorEwmBean.getOwnerAddress())){
            return;
        }
        String addressName=visitorEwmBean.getCommunityName();
        txCommunityName.setText("-" + addressName + "-");
        String sex="先生";
        if(visitorEwmBean.getGender()!=1){
            sex="女士";
        }
        txName.setText("尊敬的 " + visitorEwmBean.getSurname()+sex);
        if (!TextUtils.isEmpty(visitorEwmBean.getPlateNumber()))
            txCarNumber.setText(visitorEwmBean.getPlateNumber() + "");
        if (!TextUtils.isEmpty(visitorEwmBean.getPassCode()) && visitorEwmBean.getPassCode().length() >= 9) {
            StringBuilder sb = new StringBuilder(visitorEwmBean.getPassCode());
            sb.insert(3, "-");
            sb.insert(7, "-");
            txCode.setText(sb.toString());
        }
        txComeDate.setText("到访时间：" + visitorEwmBean.getScheduleAt());
        msg= String.format(SEND_MSG,getString(R.string.app_send_msg), Utils.getStringDate(visitorEwmBean.getScheduleAt()),addressName,txCode.getText().toString()+"");
    }


    @OnClick(R.id.tx_send)
    public void onClick() {
        if(Utils.isFastDoubleClick())return;
        if (jiepingMap == null && bitmap != null) {
            jiepingMap = Utils.takeScreenShot(this);
            myShare.setDefaultJPMap(jiepingMap, msg);//更换使用方式
        }
        myShare.setVisibility(View.VISIBLE);
    }

    protected void onDestroy() {
        super.onDestroy();
        if (jiepingMap != null) {
            jiepingMap.recycle();
        }
        if (bitmap != null) {
            bitmap.recycle();
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        if(myShare.getVisibility()==View.GONE)finish();
        else myShare.setVisibility(View.GONE);
    }
}
